package com.huawei.java.main.classes;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Day {
    private final List<Purchase> purchase;
    private final List<Deploy> deploys;
    private final List<Migration> migration;

    public Day(List<ServerInstance> purchase, List<Deploy> deploys, List<Migration> migration) {
        this.purchase = purchase.stream()
                .map(serverInstance -> new Purchase(serverInstance.type))
                .collect(Collectors.toList());
        this.deploys = deploys;
        this.migration = migration;
    }

    public List<Purchase> getPurchase() {
        return purchase;
    }

    public List<Deploy> getDeploys() {
        return deploys;
    }

    public List<Migration> getMigration() {
        return migration;
    }

    @Override
    public String toString() {
        String purchase = String.format("(purchase, %d)", this.purchase.size());
        final String purchases = this.purchase.stream()
                .map(Purchase::toString)
                .reduce((a, b) -> a + "\n" + b)
                .orElse(null);

        String migration = String.format("(migration, %d)", this.migration.size());
        final String migrations = this.migration.stream()
                .map(Migration::toString)
                .reduce((a, b) -> a + "\n" + b)
                .orElse(null);

        final String deploys = this.deploys.stream()
                .map(Deploy::toString)
                .reduce((a, b) -> a + "\n" + b)
                .orElse(null);

        return Arrays.stream(new String[]{purchase, purchases, migration, migrations, deploys})
                .filter(Objects::nonNull)
                .reduce((a, b) -> a + "\n" + b)
                .orElse("");
    }
}
